package com.springboot.cloud.demos.feign.events;

import com.alibaba.fastjson.JSONObject;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.oss.OssScanUtil;
import com.springboot.cloud.demos.entity.form.AlbumForm;
import com.springboot.cloud.demos.entity.po.Album;
import com.springboot.cloud.demos.entity.po.Moment;
import com.springboot.cloud.demos.feign.config.BusConfig;
import com.springboot.cloud.demos.feign.service.AlbumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j

/**
 * 异步处理图片鉴黄/视频鉴黄服务
 * */
public class MediaRabbitReceiver {

    @Autowired
    private OssScanUtil ossScanUtil;

    @Autowired
    private AlbumService albumService;

    @RabbitListener(queues = BusConfig.QUEUE_NAME_OSS_SCAN)
    @RabbitHandler
    public void ImageSyncScanRequest(String message) {
        log.info("----开始处理图片鉴黄----");
        log.info("收到的MQ消息：{}", message);

        Moment moment = JSONObject.parseObject(message, Moment.class);
        Result<List<Album>> result = albumService.getAlbum(moment.getId());
        if (result.isSuccess()) {
            List<Album> list = result.getData();
            for (Album album : list) {
                boolean deleted = ossScanUtil.ImageSyncScanRequest(album.getUrl());
                log.info("是否删除：{}{}", album.getId(), deleted);
                if (deleted) {
                    log.info("涉黄，删除：{}", album.getId());
                    albumService.delete(album.getId());
                } else {
                    log.info("合格，不删除：{}", album.getId());
                }
            }
        }

    }

}
